/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 10:47:36 AM
 * To change this template use File | Settings | File Templates.
 */
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Context, FieldEvaluationException, Boolexp, Regexp,
//            Schema

abstract class Field
{

    Field()
    {
    }

    abstract Element toXML(Context context);

    abstract String evaluate(Context context, Element element)
        throws FieldEvaluationException;

    Element findSelected(Context context, Boolexp boolexp)
    {
        if(boolexp == null)
            return context.this_e;
        if(error)
            return null;
        Element element = null;
        if(boolexp.evaluateMemo(context) == 1)
            element = context.e;
        for(Iterator iterator = context.e.getChildren().iterator(); iterator.hasNext();)
        {
            Element element1 = findSelected(new Context((Element)iterator.next(), context.this_e, context.v), boolexp);
            if(element1 != null)
                if(element != null)
                    error = true;
                else
                    element = element1;
            if(error)
                return null;
        }

        return element;
    }

    String fixQname(String s, Element element, boolean flag)
    {
        if(!Regexp.isPname(s) || !Schema.prefixDeclared(s, element))
            return null;
        String s1 = Schema.pnameLocal(s);
        String s2;
        if(flag)
        {
            s2 = Schema.panameURI(s, element);
            if(s2 == null)
                return s1;
        } else
        {
            s2 = Schema.penameURI(s, element);
            if(s2 == null)
                return null;
        }
        return s2 + ":" + s1;
    }

    boolean error;
}
